/*
 Copyright 2011 Dario Seitel, Sebastian Mayer. All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this list of
 conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice, this list
 of conditions and the following disclaimer in the documentation and/or other materials
 provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 The views and conclusions contained in the software and documentation are those of the
 authors and should not be interpreted as representing official policies, either expressed
 or implied, of <copyright holder>.
 */

#include "Renderer.h"
#include "../logic/GameEngine.h"
#include <iostream>

#include <SFML/Window.hpp>

Renderer::Renderer() {
	// Set color and depth clear value
	glClearDepth(1.f);
	glClearColor(0.f, 0.f, 0.f, 0.f);

	// Enable Z-buffer read and write
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);

	// light
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	GLfloat global_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
	glShadeModel(GL_SMOOTH);
	GLfloat specular[] = {1.0f, 1.0f, 1.0f , 1.0f};
	GLfloat position[] = { -1.5f, 1.0f, -4.0f, 1.0f };
	glLightfv(GL_LIGHT0, GL_DIFFUSE, specular);
	glLightfv(GL_LIGHT0, GL_POSITION, position);

	// Setup a perspective projection
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90.f, 1.f, 1.f, 500.f);
}

Renderer::~Renderer() {
	// TODO Auto-generated destructor stub
}

void Renderer::run() {
	Player &player = GameEngine::get().getPlayer();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(-player.getX(), -player.getY(), -200.f);

//	    glRotatef(50, 1.f, 0.f, 0.f);
//	    glRotatef(30, 0.f, 1.f, 0.f);
//	    glRotatef(90, 0.f, 0.f, 1.f);

	// render terrain
	for (int regionX = 0; regionX < GameEngine::get().getMapWidth(); regionX++) {
		for (int regionY = 0; regionY < GameEngine::get().getMapHeight(); regionY++) {
			Entity*** map = GameEngine::get().getRegionMap(regionX, regionY);
			for (int x = 0; x < REGION_WIDTH; x++) {
				for (int y = 0; y < REGION_HEIGHT; y++) {
					renderEntity(map[x][y]);
				}
			}
		}
	}

	// render all other objects
	list<Entity*> &renderedObjects = GameEngine::get().getRenderedObjects();
	for (list<Entity*>::iterator it = renderedObjects.begin(); it != renderedObjects.end(); it++) {
		renderEntity(*it);
	}

	if (GameEngine::get().isEnabledDebugFeature(DEBUG_FEATURE_REGION_BORDERS)) {
		GLfloat red[] = {1.0f, 0.f, 0.f, 1.f};
		glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
		glPushAttrib(GL_FRONT_AND_BACK);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glLineWidth(4);
		for (int x = player.getX() / REGION_WIDTH / MAP_TILE_SIZE - 1; x <= player.getX() / REGION_WIDTH / MAP_TILE_SIZE + 1; x++) {
			for (int y = player.getY() / REGION_HEIGHT / MAP_TILE_SIZE - 1; y <= player.getY() / REGION_HEIGHT / MAP_TILE_SIZE + 1; y++) {
				renderCube(x * REGION_WIDTH * MAP_TILE_SIZE, y * REGION_WIDTH * MAP_TILE_SIZE, 20, REGION_WIDTH * MAP_TILE_SIZE, REGION_HEIGHT * MAP_TILE_SIZE, 1);
			}
		}
		glLineWidth(1);
		glPopAttrib();
	}
}

void Renderer::renderEntity(Entity* entity) {
	Player &player = GameEngine::get().getPlayer();

	// TODO frustum size is not calculated, I just took some values that worked
	// view frustum culling
	if (entity->getX() + entity->getBoundingBoxHalfWidth() < player.getX() - 250
		|| entity->getX() - entity->getBoundingBoxHalfWidth() > player.getX() + 250
		|| entity->getY() + entity->getBoundingBoxHalfHeight() < player.getY() - 250
		|| entity->getY() - entity->getBoundingBoxHalfHeight() > player.getY() + 250)
		return;

	entity->render();

	if (GameEngine::get().isEnabledDebugFeature(DEBUG_FEATURE_COLLISION_BOXES)) {
		GLfloat red[] = {1.0f, 0.f, 0.f, 1.f};
		glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
		glPushAttrib(GL_FRONT_AND_BACK);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glLineWidth(2);
		renderCube(entity->getX(), entity->getY(), 0, entity->getBoundingBoxHalfWidth());
		glLineWidth(1);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		glPopAttrib();
	}
}

void Renderer::renderCube(float x, float y, float z, int halfWidth, int halfHeight, int halfDepth) {
	if (halfHeight == -1)
		halfHeight = halfWidth;

	if (halfDepth == -1)
		halfDepth = halfHeight;

	glBegin(GL_QUADS);
		// bottom side
		/*glNormal3d(0, 0, -1);
		glVertex3f(x - halfWidth, y - halfWidth, z - halfWidth);
		glVertex3f(x - halfWidth, y + halfWidth, z - halfWidth);
		glVertex3f(x + halfWidth, y + halfWidth, z - halfWidth);
		glVertex3f(x + halfWidth, y - halfWidth, z - halfWidth);*/

		// top
		glNormal3d(0, 0, 1);
		glVertex3f(x - halfWidth, y - halfHeight, z + halfDepth);
		glVertex3f(x - halfWidth, y + halfHeight, z + halfDepth);
		glVertex3f(x + halfWidth, y + halfHeight, z + halfDepth);
		glVertex3f(x + halfWidth, y - halfHeight, z + halfDepth);

		// left
		glNormal3d(-1, 0, 0);
		glVertex3f(x - halfWidth, y - halfHeight, z - halfDepth);
		glVertex3f(x - halfWidth, y + halfHeight, z - halfDepth);
		glVertex3f(x - halfWidth, y + halfHeight, z + halfDepth);
		glVertex3f(x - halfWidth, y - halfHeight, z + halfDepth);

		// right
		glNormal3d(1, 0, 0);
		glVertex3f(x + halfWidth, y - halfHeight, z - halfDepth);
		glVertex3f(x + halfWidth, y + halfHeight, z - halfDepth);
		glVertex3f(x + halfWidth, y + halfHeight, z + halfDepth);
		glVertex3f(x + halfWidth, y - halfHeight, z + halfDepth);

		// front
		glNormal3d(0, -1, 0);
		glVertex3f(x - halfWidth, y - halfHeight, z + halfDepth);
		glVertex3f(x - halfWidth, y - halfHeight, z - halfDepth);
		glVertex3f(x + halfWidth, y - halfHeight, z - halfDepth);
		glVertex3f(x + halfWidth, y - halfHeight, z + halfDepth);

		// back
		glNormal3d(0, 1, 0);
		glVertex3f(x - halfWidth, y + halfHeight, z + halfDepth);
		glVertex3f(x - halfWidth, y + halfHeight, z - halfDepth);
		glVertex3f(x + halfWidth, y + halfHeight, z - halfDepth);
		glVertex3f(x + halfWidth, y + halfHeight, z + halfDepth);
	glEnd();
}

void Renderer::renderPlane(float x, float y, float z, int halfWidth) {
	glBegin(GL_QUADS);
		glNormal3d(0, 0, 1);
		glVertex3f(x - halfWidth, y - halfWidth, z - halfWidth);
		glVertex3f(x - halfWidth, y + halfWidth, z - halfWidth);
		glVertex3f(x + halfWidth, y + halfWidth, z - halfWidth);
		glVertex3f(x + halfWidth, y - halfWidth, z - halfWidth);
	glEnd();
}

